<template>
	<view class="container">
		<view class="nameView">
			<view class="ticketName">{{groupName}}</view>
			<rich-text :nodes="desc"></rich-text>
		</view>
		<view class="ticketList">
			<view class="ticketTitle">
				<image src="../../static/img/ticket_icon.png" mode="widthFix"></image>
				门票
			</view>
			<view class="ticketItem" v-for="(item,index) in ticketList">
				<view class="itemLeft">
					<view class="name">{{item.ticketName}}</view>
					<view class="desc">{{item.description}}</view>
				</view>
				<view class="itemRight">
					<view class="price">{{item.settlePrice}}元</view>
					<view class="buyBtn" @click="buyClick(item.ticketInfoId)">立即预定</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default{
		data () {
			return{
				sale: true,
				desc: '',
				groupName: '',
				ticketList: [],
				ticketGroupNum: ''
			}
		},
		onLoad(options) {
			let that = this;
			console.log(options);
			let ticketGroupNum = options.ticketGroupNum;
			that.ticketGroupNum = ticketGroupNum;
			uni.request({
				url: 'https://boss.smart-ideas.com.cn/ticketApi/ticketInfo/getSalesList',
				data: {
				  enterpriseCode: uni.getStorageSync('enterpriseCode'),
				  ticketGroupNum: ticketGroupNum,
				  ticketSalesChannelsNum: 'MINI'
				},
				method: 'GET',
				success: (res) => {
					console.log(res);
					that.groupName = res.data.data.groupName;
					let ticketList = res.data.data.ticketSalesInfoList;
					that.ticketList = ticketList;
					if(res.data.code == '10000'){
						that.sale = true;
					}else{
						that.sale = false;
					}
				}
			})
		},
		methods: {
			buyClick(ticketId){
				let that = this;
				uni.request({
					url: 'https://boss.smart-ideas.com.cn/ticketApi/ticketInfo/getSalesList',
					data: {
					  enterpriseCode: uni.getStorageSync('enterpriseCode'),
					  ticketGroupNum: that.ticketGroupNum,
					  ticketSalesChannelsNum: 'MINI'
					},
					method: 'GET',
					success: (res) => {
						if(res.data.code == '40002'){
							uni.showModal({
								content: '已闭园，暂不售票'
							})
							return;
						}
						let ticketList = res.data.data.ticketSalesInfoList;
						let buyRules;
						ticketList.forEach(function(value, key){
							if(value.ticketInfoId == ticketId){
								buyRules = value.buyRules;
							}
						})
						let saleStartDate = buyRules.salesStartDate.replace(/-/, '/').replace(/-/, '/') + ' 00:00:00';
						let salesEndDate = buyRules.salesEndDate.replace(/-/, '/').replace(/-/, '/') + ' 23:59:59';
						let nowDate = new Date().getTime();
						if(nowDate < new Date(saleStartDate) || nowDate > new Date(salesEndDate)){
							uni.showModal({
								content: '暂不售票，不在售票时间'
							})
							return;
						}
						uni.navigateTo({
							url: '/pages/ticket/ticket?ticketInfoId=' + ticketId
						})
					}
				})
			}
		}
	}
</script>

<style>
	.container{
		box-sizing: border-box;
		min-height: 100vh;
		background-color: #f5f5f5;
	}
	
	.nameView{
		box-sizing: border-box;
		background-color: #fff;
		padding: 10px 15px;
		border-bottom: 1px solid #f5f5f5;
	}
	
	
	.ticketList{
		padding: 0 20rpx;
		margin-top: 20rpx;
		background-color: #fff;
	}
	.ticketTitle{
		display: flex;
		justify-content: flex-start;
		align-items: center;
		font-size: 30rpx;
		color: #333;
		font-weight: bold;
		padding: 20rpx;
		border-bottom: 1px solid #f5f5f5;
	}
	.ticketTitle image{
		width: 50rpx;
		height: 30rpx;
		margin-right: 20rpx;
	}
	.ticketItem{
		box-sizing: border-box;
		width: 100%;
		padding: 20rpx;
		display: flex;
		justify-content: space-between;
		align-items: flex-start;
		border-bottom: 1px solid #f5f5f5;
	}
	.ticketItem .itemLeft{
		flex: 1;
	}
	.ticketItem .itemLeft .name{
		font-size: 32rpx;
		color: #333;
		line-height: 46rpx;
		margin-bottom: 10rpx;
	}
	.ticketItem .itemLeft .desc{
		font-size: 26rpx;
		color: #666;
		line-height: 40rpx;
	}
	.ticketItem .itemRight{
		flex: 0 0 150rpx;
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
	}
	.ticketItem .itemRight .price{
		font-size: 40rpx;
		color: #ff5500;
		line-height: 46rpx;
	}
	.ticketItem .itemRight .buyBtn{
		background-color: #00B3FD;
		padding: 10rpx;
		border-radius: 5rpx;
		font-size: 28rpx;
		color: #fff;
		width: 150rpx;
		height: 45rpx;
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		margin-top: 10rpx;
		letter-spacing: 7rpx;
		text-align: center;
		border-radius: 45rpx;
	}
	.ticketItem .itemRight .buyBtn.disbled{
		background-color: #ddd;
		color: #666;
	}
</style>